/**
 * 
 */
package org.uni.cil.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Logger;

//import org.uni.cil.benchmark.Benchmark;
import org.uni.cil.benchmark.*;
import org.uni.cil.crm.CRMConnector;
import org.uni.cil.mail.MailGetter;
import javax.servlet.jsp.JspWriter;

/**
 * @author Elmar
 */
public class Controller {

	//private static CRMConnector crmConnect = new CRMConnector();
	private MailGetter mailGetter;
	private Benchmark benchmarker;
	private int pingTime;
	public Controller () {
	
	}
	
	// TODO implement a method which includes the logic flow of the application
	// TODO define tuple class instead of HashMap with singleton element
	public void run(JspWriter writer, HashMap<String, String> mailConnectionData, String crmProvider, String[] searchTokens, String[] searchText) {
	
		String username = mailConnectionData.get("username");
		String password = mailConnectionData.get("password");
		String hostname = mailConnectionData.get("hostname");
		String hosttype = mailConnectionData.get("hosttype");

		PipedInputStream  pipeIn = new PipedInputStream();
        PipedOutputStream pipeOut = null;
		try {
			pipeOut = new PipedOutputStream( pipeIn );
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		Benchmark downloadBenchmarker = new Benchmark(hostname, "CASPia", "Upload");
		mailGetter = new MailGetter(writer, pipeOut, downloadBenchmarker);	
		
 		// set up the benchmark object
		// TODO add tagging to constructor to benchmark like mail-provider etc.
		benchmarker = new Benchmark(hostname, "CASPia", "Main");
		benchmarker.start();
		//pingTime = benchmarker.ping(hostname);
		// Connect to mailbox
		mailGetter.connect(username, password, hostname, hosttype);
		// Prepare search input
		List<HashMap<MailGetter.SearchToken, String>> searchList = null;
		try {
			searchList = MailGetter.getSearchList(searchTokens, searchText);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// start timing
		//benchmarker.start();
		// get mails

		if (searchList != null && searchList.size() > 0)
		{
			mailGetter.setSearchList(searchList);
		}
		mailGetter.start();
		// Stop time for mail getting
		benchmarker.split("finished mail download");
		
		// call the CRMConnector class which puts the mails into the crm system
		// split time
		// start transferring emails (filtered by keywords, addresses etc)
		// saves in a variable whether the action was successfull
		// stopTime
		// transfer times to csv
				
		Benchmark uploadBenchmarker = new Benchmark(hostname, "CASPia", "Upload");
		CRMConnector crm = new CRMConnector(pipeIn, uploadBenchmarker, crmProvider, writer);
		crm.run();
		
		benchmarker.stop("finished mail upload", -1);
		String pathMainRes = benchmarker.toCSV();
		String pathUploadRes = uploadBenchmarker.toCSV();
		//String pathDownloadRes = downloadBenchmarker.toCSV();
		try {
			writer.println("<br><a href='" + pathMainRes + "' >Download Main-Benchmark</a>");
			writer.println("<br><a href='" + pathUploadRes + "' >Download Upload-Benchmark</a>");
			//writer.println("<br>"+pingTime);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

}
